package com.qxzn.app.util;

import android.app.Activity;
import android.text.TextUtils;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Stack;

/**
 * activity栈管理
 */
public class ActivityStack {

    private static Activity topActivity = null;
    private static Stack<Activity> activities = new Stack<Activity>();

    public static Activity getTargetActivity() {
        if (topActivity != null) {
            return topActivity;
        }
        return null;
    }

    //添加目标Activity
    public static void setTargetActivity(Activity activity) {
        if (topActivity == null) {
            topActivity = activity;
        }
    }

    //移除掉目标Activity
    public static void removeTargetActivity() {
        if (topActivity != null) {
            removeActivity(topActivity);
            topActivity = null;
        }
    }

    public static void addActivity(Activity activity) {
        if (activity != null) {
            activities.add(activity);
            AppUtil.showLog("添加" + activity.getClass().getSimpleName());
            showAllActivity();
        }
    }

    public static void removeActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            activities.remove(activity);
            AppUtil.showLog("移除" + activity.getClass().getSimpleName());
            showAllActivity();
        }
    }

    public static Activity currentActivity() {
        try {
            if (!activities.lastElement().isFinishing()) {
                return activities.lastElement();
            }
        } catch (NoSuchElementException e) {
        }
        return null;
    }

    public static String currentActivityName() {
        Activity activity = currentActivity();
        if (activity == null) {
            return "";
        } else {
            return activity.getClass().getName();
        }
    }

    public static String currentActivitySimpleName() {
        Activity activity = currentActivity();
        if (activity == null) {
            return "";
        } else {
            return activity.getClass().getSimpleName();
        }
    }

    //结束所有Activity
    public static void finishAllActivity() {
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
            }
        }
        activities.clear();
        AppUtil.showLog("移除所有Activity操作");
    }

    //结束所有Activity
    public static void finishAllWithoutActivity(String... className) {
        Iterator<Activity> iterator = activities.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity != null) {
                boolean needFinish = true;
                for (String name : className) {
                    if (TextUtils.equals(name, activity.getClass().getName())) {
                        needFinish = false;
                        break;
                    }
                }
                if (needFinish) {
                    activity.finish();
                    iterator.remove();
                }
            }
        }
        AppUtil.showLog("移除所有Activity操作，保留activity：" + className);
        showAllActivity();
    }

    public static void showAllActivity() {
        removeUnvalidActivity();

        StringBuilder stringBuilder = new StringBuilder();
        for (Activity activity : activities) {
            stringBuilder.append(activity.getClass().getSimpleName() + "，");
        }
        AppUtil.showLog("所有Activity为：" + stringBuilder);
    }

    /**
     * 移除无效activity（可能被系统回收）
     */
    private static void removeUnvalidActivity() {
        Iterator<Activity> iterator = activities.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity == null) {
                iterator.remove();
                AppUtil.showLog("移除已销毁的activity");
            } else {
                if (activity.isFinishing()) {
                    iterator.remove();
                    AppUtil.showLog("移除已销毁的activity：" + activity.getClass().getName());
                }
            }
        }
    }

    public static Stack<Activity> getActivities() {
        return activities;
    }

    public static boolean isContainsActivity(Activity activity) {
        try {
            for (Activity activity1 : activities) {
                if (TextUtils.equals(activity1.getClass().getName(), activity.getClass().getName())) {
                    return true;
                }
            }
        } catch (Exception e) {

        }
        return false;
    }

    public static boolean isContainsActivity(String activity) {
        try {
            for (Activity activity1 : activities) {
                if (TextUtils.equals(activity1.getClass().getName(), activity)) {
                    return true;
                }
            }
        } catch (Exception e) {

        }
        return false;
    }

    public static void finishActivityByName(String activity) {
        try {
            for (Activity activity1 : activities) {
                if (TextUtils.equals(activity1.getClass().getName(), activity)) {
                    activity1.finish();
                    AppUtil.showLog("关闭：" + activity);
                    return;
                }
            }
        } catch (Exception e) {

        }
    }

}
